<%@page pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>达内－NetCTOSS</title>
        <link type="text/css" rel="stylesheet" media="all" href="../styles/global.css" />
        <link type="text/css" rel="stylesheet" media="all" href="../styles/global_color.css" />
        <script language="javascript" type="text/javascript" src="../js/jquery-1.4.3.js">
        </script>
        <script language="javascript" type="text/javascript">
        	var chose;
        	var nameFlag = false;
        	var durationFlag = false;
        	var baseCostFlag = false;
        	var unitCostFlag = false;
        	var descrFlag = false;
        	//页面加载完执行
        	$(function(){
        		//给资费名的文本框的光标移除事件绑定函数
        		$("#costName").blur(function(){
        			//当资费名光标移除时触发的代码
        			var name = $(this).val();
        			//先校验是否为空
        			if(name==null||name==""){
        				nameFlag = false;
        				$("#costNameMsg").text("资费名称不能为空！");
        				$("#costNameMsg").addClass("error_msg");
        				return;
        			}
        			//校验输入是否正确
        			var rgexp = /[\w\u4e00-\u9fff_]{1,50}$/g;
        			var isTrue = rgexp.test(name);
        			if(!isTrue){
        				nameFlag = false;
        				$("#costNameMsg").text("输入的内容不符合要求");
        				$("#costNameMsg").addClass("error_msg");
        				return;
        			}
        			//校验是否重复
        			$.post(
        				"checkRepeat",
        				{"name":name},
        				function(data){
        					var repeat = data;
        					if(repeat){
        						//重复的情况，提示
        						$("#costNameMsg").text("资费名称已存在！");
        						$("#costNameMsg").addClass("error_msg");
        						nameFlag = false;
        					}else{
        						//没重复的情况
        						$("#costNameMsg").text("该资费名可以使用");
        						$("#costNameMsg").removeClass("error_msg");
        						nameFlag = true;
        					}
        				}
        			);
        		});
        		//基本时长
        		$("#baseDuration").blur(function(){
        			if(chose!=1&&chose!=3){
	        			var duration = $(this).val();
	        			if(duration==null||duration==""){
	        				durationFlag = false;
	        				$("#durationMsg").text("基本时长不能为空");
	        				$("#durationMsg").addClass("error_msg");
	        				return;
	        			}
	        			if(duration<1 || duration>600 || parseInt(duration)!=duration){
	        				durationFlag = false;
	        				$("#durationMsg").text("请输入1～600间的整数！");
	        				$("#durationMsg").addClass("error_msg");
	        				return;
	        			}else{
	        				$("#durationMsg").text("输入正确");
	        				$("#durationMsg").removeClass("error_msg");
	        				durationFlag = true;
	        			}
        			}
        		});
        		//基本费用
        		$("#baseCost").blur(function(){
        			if(chose!=3){
	        			var baseCost = $(this).val();
	        			if(baseCost==null||baseCost==""){
	        				baseCostFlag = false;
	        				$("#baseCostMsg").text("基本费用不能为空！");
	        				$("#baseCostMsg").addClass("error_msg");
	        				return;
	        			}
	        			if(baseCost<1 || baseCost*100>9999999 || parseInt(baseCost*100)!=(baseCost*100)){
	        				baseCostFlag = false;
	        				$("#baseCostMsg").text("请输入1～99999.99间的整数！");
	        				$("#baseCostMsg").addClass("error_msg");
	        				return;
	        			}else{
	        				$("#baseCostMsg").text("输入正确");
	        				$("#baseCostMsg").removeClass("error_msg");
	        				baseCostFlag = true;
	        			}
        			}
        		});
        		//单位费用
        		$("#unitCost").blur(function(){
        			var unitCost = $(this).val();
        			if(chose!=1){
	        			if(unitCost==null||unitCost==""){
	        				unitCostFlag = false;
	        				$("#unitCostMsg").text("单位费用不能为空！");
	        				$("#unitCostMsg").addClass("error_msg");
	        				return;
	        			}
	        			if(unitCost<1 || unitCost*100>9999999 || parseInt(unitCost*100)!=(unitCost*100)){
	        				unitCostFlag = false;
	        				$("#unitCostMsg").text("请输入1～99999.99间的整数！");
	        				$("#unitCostMsg").addClass("error_msg");
	        				return;
	        			}else{
	        				$("#unitCostMsg").text("输入正确");
	        				$("#unitCostMsg").removeClass("error_msg");
	        				unitCostFlag = true;
	        			}
        			}
        		});
        		//资费说明descr
        		$("#descr").blur(function(){
        			var descr = $(this).val();
        			if(descr==null||unitCost==""){
        				descrFlag = false;
        				$("#descrMsg").text("资费说明不能为空！");
        				$("#descrMsg").addClass("error_msg");
        				return;
        			}
        			var rgexp = /[\w\u4e00-\u9fff_]{1,100}$/g;
        			var isTrue = rgexp.test(descr);
        			if(!isTrue){
        				descrFlag = false;
        				$("#descrMsg").text("输入的内容不符合要求");
        				$("#descrMsg").addClass("error_msg");
        				return;
        			}else{
        				$("#descrMsg").text("输入正确");
        				$("#descrMsg").removeClass("error_msg");
        				descrFlag = true;
        			}
        		});
        	});    
        
        	//保存结果的提示
            function showResult() {
        		//判断资费名是否重复
        		if(!nameFlag || !durationFlag || !baseCostFlag || !unitCostFlag){
              		showResultDiv(true);
             		window.setTimeout("showResultDiv(false);", 3000);
        			return;
        		}
        		var flag1 = 0;
		       	var checkobj=document.getElementById("CheckBoxList");
		        var checks = checkobj.getElementsByTagName("input");
		        for(var n = 0; n<checks.length;n++){
	            	if( checks[n].checked==true){
	            		flag1=1;
		           	}
	            }
	            if(flag1==0){
	            	$("#save_result_info").text("请至少选择一项资费类型");
	                showResultDiv(true);
	                window.setTimeout("showResultDiv(false);", 3000);
	            	return;
	            }
            	document.forms[0].submit();
            }
            function showResultDiv(flag) {
                var divResult = document.getElementById("save_result_info");
                if (flag)
                    divResult.style.display = "block";
                else
                    divResult.style.display = "none";
            }

            //切换资费类型
            function feeTypeChange(type) {
            	chose = type;
                var inputArray = document.getElementById("main").getElementsByTagName("input");
                if (type == 1) {
                    inputArray[4].readOnly = true;
                    inputArray[4].value = "";
                    inputArray[4].className += " readonly";
                    inputArray[5].readOnly = false;
                    inputArray[5].className = "width100";
                    inputArray[6].readOnly = true;
                    inputArray[6].className += " readonly";
                    inputArray[6].value = "";
                    $("#durationMsg").text("无需输入").removeClass("error_msg");
                    $("#baseCostMsg").text("0-99999.99之间的数值").removeClass("error_msg");
                    $("#unitCostMsg").text("无需输入").removeClass("error_msg");
                    durationFlag = true;
                    unitCostFlag = true;
                }
                else if (type == 2) {
                    inputArray[4].readOnly = false;
                    inputArray[4].className = "width100";
                    inputArray[5].readOnly = false;
                    inputArray[5].className = "width100";
                    inputArray[6].readOnly = false;
                    inputArray[6].className = "width100";
                    $("#durationMsg").text("1-600之间的整数").removeClass("error_msg");
                    $("#baseCostMsg").text("0-99999.99之间的数值").removeClass("error_msg");
                    $("#unitCostMsg").text("0-99999.99之间的数值").removeClass("error_msg");
                }
                else if (type == 3) {
                    inputArray[4].readOnly = true;
                    inputArray[4].value = "";
                    inputArray[4].className += " readonly";
                    inputArray[5].readOnly = true;
                    inputArray[5].value = "";
                    inputArray[5].className += " readonly";
                    inputArray[6].readOnly = false;
                    inputArray[6].className = "width100";
                    $("#durationMsg").text("无需输入").removeClass("error_msg");
                    $("#baseCostMsg").text("无需输入").removeClass("error_msg");
                    $("#unitCostMsg").text("0-99999.99之间的数值").removeClass("error_msg");
                    durationFlag = true;
                    baseCostFlag = true;
                }
            }
        </script>
    </head>
    <body>
        <!--Logo区域开始-->
        <div id="header">
            <img src="../images/logo.png" alt="logo" class="left"/>
            <a href="#">[退出]</a>            
        </div>
        <!--Logo区域结束-->
        <!--导航区域开始-->
        <div id="navi">
            <ul id="menu">
                <li><a href="../index/index" class="index_off"></a></li>
                <li><a href="../role/findRole" class="role_off"></a></li>
                <li><a href="../admin/findAdmin" class="admin_off"></a></li>
                <li><a href="../cost/findCost" class="fee_on"></a></li>
                <li><a href="../account/findAccount" class="account_off"></a></li>
                <li><a href="../service/findService" class="service_off"></a></li>
                <li><a href="../bill/findBill" class="bill_off"></a></li>
                <li><a href="../report/findReport" class="report_off"></a></li>
                <li><a href="../user/userInfo" class="information_off"></a></li>
                <li><a href="../user/userModiPwd" class="password_off"></a></li>
            </ul>
        </div>
        <!--导航区域结束-->
        <!--主要区域开始-->
        <div id="main">            
            <div id="save_result_info" class="save_fail">保存失败，信息填写有误！</div>
            <form action="addCost" method="post" class="main_form">
                <div class="text_info clearfix"><span>资费名称：</span></div>
                <div class="input_info">
                	<s:textfield name="cost.name" cssClass="width300" id="costName"></s:textfield>
                    <span class="required">*</span>
                    <div class="validate_msg_short" id="costNameMsg">50长度的字母、数字、汉字和下划线的组合</div>
                </div>
                <div class="text_info clearfix"><span>资费类型：</span></div>
                <div class="input_info fee_type" id="CheckBoxList">
                	<s:radio name="cost.costType" list="#{'1':'包月','2':'套餐','3':'计时'}" onclick='feeTypeChange(this.value);'></s:radio>
                </div>
                <div class="text_info clearfix"><span>基本时长：</span></div>
                <div class="input_info">
                	<s:textfield name="cost.BaseDuration" cssClass="width100" id="baseDuration"></s:textfield>
                    <span class="info">小时</span>
                    <span class="required">*</span>
                    <div class="validate_msg_long" id="durationMsg">1-600之间的整数</div>
                </div>
                <div class="text_info clearfix"><span>基本费用：</span></div>
                <div class="input_info">
                	<s:textfield name="cost.baseCost" cssClass="width100" id="baseCost"></s:textfield>
                    <span class="info">元</span>
                    <span class="required">*</span>
                    <div class="validate_msg_long" id="baseCostMsg">0-99999.99之间的数值</div>
                </div>
                <div class="text_info clearfix"><span>单位费用：</span></div>
                <div class="input_info">
                	<s:textfield name="cost.unitCost" cssClass="width100" id="unitCost"></s:textfield>
                    <span class="info">元/小时</span>
                    <span class="required">*</span>
                    <div class="validate_msg_long" id="unitCostMsg">0-99999.99之间的数值</div>
                </div>
                <div class="text_info clearfix"><span>资费说明：</span></div>
                <div class="input_info_high">
                	<s:textfield name="cost.descr" cssClass="width300 height70" id="descr"></s:textfield>
                    <div class="validate_msg_short" id="descrMsg">100长度的字母、数字、汉字和下划线的组合</div>
                </div>                    
                <div class="button_info clearfix">
                    <input type="button" value="保存" class="btn_save" onclick="showResult();" />
                    <input type="button" value="取消" class="btn_save" onclick="javascript:history.go(-1)"/>
                </div>
            </form>  
        </div>
        <!--主要区域结束-->
        <div id="footer">
            <span>[源自北美的技术，最优秀的师资，最真实的企业环境，最适用的实战项目]</span>
            <br />
            <span>版权所有(C)加拿大达内IT培训集团公司 </span>
        </div>
    </body>
</html>
